﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KorleBu.Application_Classes;
using System.Drawing;

namespace KorleBu
{
    public partial class RequisitionStoreManager : System.Web.UI.Page
    {

        #region variables
        DataSet ds = new DataSet();
        Presentation presentation = new Presentation();
        public DataSet items = new DataSet();
        public DataSet itemAdjustmenter = new DataSet();
        public DataSet itemAllocation = new DataSet();
        public Application_Classes.ItemSearch itemSearch = new Application_Classes.ItemSearch();
        public Application_Classes.ItemAdd IA = new Application_Classes.ItemAdd();
        public Application_Classes.ItemReady IR = new Application_Classes.ItemReady();
        private int req, proc;
        #endregion

        #region Page Load
        protected void Page_Load(object sender, EventArgs e)
        {
            //keep clean error message on load; other methods occur after load so they will fill in the error as needed.
            lblMasterError.Text = "";

            if (!IsPostBack)
            {
                ds = presentation.GetAllItemsForStoreID(GetStoreID(), 1);
                if (ds.Tables[0].Rows[0][0].ToString() == "")
                {
                    Session.Remove("ItemAllocating");
                    itemAllocation = ds;
                    ItemAllocating();
                    populateGridviewByName(itemAllocation);

                    Session.Remove("ItemAdjuster");
                    itemAdjustmenter = ds;
                    ItemAdjuster();
                    populateItemAdjustments(itemAdjustmenter);
                    
                    lblAllocationTabError.Text = lblErrorItemAdjustment.Text = "";
                }
                else
                {
                    lblAllocationTabError.Text = lblErrorItemAdjustment.Text = "There are no Items for this store";
                    gvItemAdjustment.DataSource = null;
                    gvItemAdjustment.DataBind();
                }

                ds = presentation.GetCategoryByStore(GetStoreID());
                ddlItemCategory.DataSource = ds.Tables[1];
                ddlItemCategory.DataTextField = "itemCategoryName";
                ddlItemCategory.DataValueField = "itemCategoryID";
                ddlItemCategory.DataBind();

                ddlOutsideItemCategory.DataSource = ds.Tables[1];
                ddlOutsideItemCategory.DataTextField = "itemCategoryName";
                ddlOutsideItemCategory.DataValueField = "itemCategoryID";
                ddlOutsideItemCategory.DataBind();

                ds = presentation.GetVendors();
                ddlOutsideVendor.DataSource = ds.Tables[1];
                ddlOutsideVendor.DataTextField = "vendorName";
                ddlOutsideVendor.DataValueField = "vendorID";
                ddlOutsideVendor.DataBind();

                //TODO: ddlSubUnit being bound here.
                //ds = presentation.GetSubUnits();
                //ddlSubUnit.DataSource = ds.Tables[1];
                //ddlSubUnit.DataTextField = "subUnitName";
                //ddlSubUnit.DataValueField = "subUnitID";
                //ddlSubUnit.DataBind();
            }

            ds = presentation.CheckOpenRequisition(GetStoreID());

            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                req = Convert.ToInt32(ds.Tables[1].Rows[0][0].ToString());
                Session.Remove("Req");
                OpenReq();
                lblRequisitionID.Text = req.ToString();
                lblRequisitionDate.Text = ds.Tables[1].Rows[0][1].ToString();
                //lblDescription.Text = ds.Tables[1].Rows[0][2].ToString();
                mvRequest.ActiveViewIndex = 1;
                ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(lblRequisitionID.Text), false);

                //Adam: added !IsPostBack because GridView's get super cranky with Validation otherwise and just blow up the page.
                //We never saw the blow up because the call was ajax.
                if (!IsPostBack)
                {
                    if (ds.Tables[0].Rows[0][0].ToString() == "")
                    {
                        gvRequisition.DataSource = ds.Tables[1];
                        gvRequisition.DataBind();
                        btnSubmitRequisition.Visible = true;
                        lblRequestMissing.Text = "";
                    }
                    else
                    {
                        btnSubmitRequisition.Visible = false;
                        lblRequestMissing.Text = "There are currently no items for this request";
                    }
                }
            }
            else
            {
                ds = presentation.CheckOpenOutsideProcurement(GetStoreID());

                if (ds.Tables[0].Rows[0][0].ToString() == "")
                {
                    proc = Convert.ToInt32(ds.Tables[1].Rows[0][0].ToString());
                    Session.Remove("Proc");
                    OpenProc();
                    lblOutsideID.Text = proc.ToString();
                    mvRequest.ActiveViewIndex = 2;
                    ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(lblOutsideID.Text), false);

                    //Adam: added !IsPostBack because GridView's get super cranky with Validation otherwise and just blow up the page.
                    //We never saw the blow up because the call was ajax.
                    if (!IsPostBack)
                    {
                        if (ds.Tables[0].Rows[0][0].ToString() == "")
                        {
                            gvOutsideRequisition.DataSource = ds.Tables[1];
                            gvOutsideRequisition.DataBind();
                            btnSubmitOutsideProcure.Visible = true;
                            lblOutsideRequestMissing.Text = "";
                        }
                        else
                        {
                            btnSubmitOutsideProcure.Visible = false;
                            lblOutsideRequestMissing.Text = "There are currently no items for this request";
                        }
                    }
                }
                else
                {
                    mvRequest.ActiveViewIndex = 0;
                }
            }
           
            ds = presentation.GetPendingRequests(GetStoreID());
            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                gvRequests.DataSource = ds.Tables[1];
                gvRequests.DataBind();
                lblViewRequestEmpty.Text = "";
            }
            else
            {
                gvRequests.DataSource = null;
                gvRequests.DataBind();
                lblViewRequestEmpty.Text = "No Request";
            }




            ds = presentation.GetRequestReady(GetStoreID());
            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                lblErrorFillRequest.Text = "";
                gvRequestFill.DataSource = ds.Tables[1];
                gvRequestFill.DataBind();
            }
            else
            {
                gvRequestFill.DataSource = null;
                gvRequestFill.DataBind();
                lblErrorFillRequest.Text = "No Request with Items ready";
            }

            ds = presentation.GetRequisitionsNotClosedByStore(GetStoreID());
            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                lblMissingRequisition.Text = "";
                gvViewRequisition.DataSource = ds.Tables[1];
                gvViewRequisition.DataBind();

                foreach (GridViewRow gvr in gvViewRequisition.Rows)
                {
                    Label lblStatusName = gvr.FindControl("lblStatusName") as Label;
                    String status = lblStatusName.Text;
                    if (status == "Pending")
                    {
                        lblStatusName.ForeColor = Color.DarkOrange;
                    }
                    else if (status == "Approved By GM" || status == "Approved By CEO")
                    {
                        lblStatusName.ForeColor = Color.DarkGreen;
                    }
                    else if (status == "Closed")
                    {
                        lblStatusName.ForeColor = Color.Red;
                    }
                }
            }
            else
            {
                lblMissingRequisition.Text = "No Requisitions are opened";
            }

            lblLoggedInUser.Text = "You are logged in as " + GetFirstName() + " " + GetLastName();

            #region Aarons stuff
            //show a message for pending tasks if the given user has tasks to do.  
            //This should only apply to store managers, general managers, CEOs, and Procurement
            //show a message for pending tasks if the given user has tasks to do.  
            //This should only apply to store managers, general managers, CEOs, and Procurement
            if (GetIsStoreManager())
            {
                ds = presentation.GetPendingRequests(GetStoreID());
                if (ds.Tables.Count > 0) // If the method returned successfully
                {
                    if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                    {
                        //lblSideBarContent.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                    }
                    else
                    {
                        //lblSideBarContent.Text = "**You have pending satellite store requests to handle.**";
                    }
                }
            }
            else if (GetIsGeneralManager())
            {
                ds = presentation.GetRequisitionsByPendingStatus();
                if (ds.Tables.Count > 0) // If the method returned successfully
                {
                    if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                    {
                        //lblSideBarContent.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                    }
                    else
                    {
                        //lblSideBarContent.Text = "**You have pending requisitions to handle.**";
                    }
                }
            }
            else if (GetIsCEO())
            {
                ds = presentation.GetRequisitionsByGMStatus();
                if (ds.Tables.Count > 0) // If the method returned successfully
                {
                    if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                    {
                        //lblSideBarContent.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                    }
                    else
                    {
                        //lblSideBarContent.Text = "**You have pending requisitions to handle.**";
                    }
                }
            }
            else if (GetIsProcurement())
            {
                ds = presentation.GetRequisitionsByCEOStatus();
                if (ds.Tables.Count > 0) // If the method returned successfully
                {
                    if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                    {
                        //lblSideBarContent.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                    }
                    else
                    {
                        //lblSideBarContent.Text = "**You have pending requisition orders to handle.**";
                    }
                }
            }
            #endregion
        }
#endregion

        #region Buttons
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            gvItemSearch.EditIndex = -1;
            Session.Remove("ItemName");
            Session.Remove("Items");
            itemSearch.searchMethod = 1;
            itemSearch.categorySearched = Convert.ToInt32(ddlItemCategory.SelectedValue);
            GetItemName();
            ds = presentation.GetItemByCategoryByStore(itemSearch.categorySearched, GetStoreID());
            items = ds;
            ItemList();
            populateItemGridView(items);

        }

        protected void btnSearchItemName_Click(object sender, EventArgs e)
        {
            gvItemSearch.EditIndex = -1;
            Session.Remove("ItemName");
            Session.Remove("Items");
            itemSearch.searchMethod = 2;
            itemSearch.itemSearched = txtItemName.Text;
            GetItemName();
            ds = presentation.GetItemByNameByStore(itemSearch.itemSearched, GetStoreID());
            items = ds;
            ItemList();
            populateItemGridView(items);
        }
        
        protected void btnCreateRequest_Click(object sender, EventArgs e)
        {
            ds = presentation.AddRequisition(Convert.ToInt32(GetStoreID()), "Need to change description");
            txtDescription.Text = "";
            mvRequest.ActiveViewIndex = 1;
        }

        protected void btnRemoveDenied_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow itemRow in gvViewRequisitionItems.Rows)
            {
                Label lblItemStatus = itemRow.FindControl("lblItemStatus") as Label;
                String itemStatus = lblItemStatus.Text;
                if (itemStatus.ToUpper().Contains("DENIED") || itemStatus.ToUpper().Contains("CLOSED"))    //ignore case
                {
                    presentation.AcceptRejectRequisitionItem(Convert.ToInt32(lblRequisitionID.Text), Convert.ToInt32((itemRow.FindControl("lblItemID") as Label).Text), true);
                }
            }
        }

        protected void btnHideRequisition_Click(object sender, EventArgs e)
        {
            String reqIdStr = lblViewRequisitionID.Text;
            int requisitionId = Convert.ToInt32(reqIdStr);
            presentation.AddCommentToRequsition(requisitionId, "");

            // Re-populate the requisitions gridview
            ds = presentation.GetRequisitionsNotClosedByStore(GetStoreID());
            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                lblMissingRequisition.Text = "";
                gvViewRequisition.DataSource = ds.Tables[1];
                gvViewRequisition.DataBind();

                foreach (GridViewRow gvr in gvViewRequisition.Rows)
                {
                    Label lblStatusName = gvr.FindControl("lblStatusName") as Label;
                    String status = lblStatusName.Text;
                    if (status == "Pending")
                    {
                        lblStatusName.ForeColor = Color.DarkOrange;
                    }
                    else if (status == "Approved By GM" || status == "Approved By CEO")
                    {
                        lblStatusName.ForeColor = Color.DarkGreen;
                    }
                    else if (status == "Closed")
                    {
                        lblStatusName.ForeColor = Color.Red;
                    }
                }
            }
            else
            {
                lblMissingRequisition.Text = "No Requisitions are opened";
                gvViewRequisition.DataSource = null;    // Gets rid of the gridview if it's now empty
                gvViewRequisition.DataBind();
            }
        }

        protected void btnSelectNewRequisition_Click(object sender, EventArgs e)
        {
            mvViewRequisition.ActiveViewIndex = 0;
        }

        protected void btnNewFillRequest_Click(object sender, EventArgs e)
        {
            mvRequestFill.ActiveViewIndex = 0;
        }

        protected void btnNewRequest_Click(object sender, EventArgs e)
        {
            lblViewRequestError.Text = "";
            mvViewRequest.ActiveViewIndex = 0;
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            ds = presentation.GetRequestItemsByRequestByStore(Convert.ToInt32(lblViewRequestID.Text), GetStoreID());
            gvRequestItems.DataSource = ds.Tables[1];
            gvRequestItems.DataBind();

            // Color each item's status
            foreach (GridViewRow gvr in gvRequestItems.Rows)
            {
                Label lblStatusName = gvr.FindControl("lblStatusName") as Label;
                String status = lblStatusName.Text;
                if (status == "Pending") { 
                    lblStatusName.ForeColor = Color.DarkOrange;
                    lblStatusName.BackColor = Color.PeachPuff; 
                }
                if (status == "Ready") { lblStatusName.ForeColor = Color.DarkGreen; }
            }

            mvViewRequest.ActiveViewIndex = 1;
            lblReallocateError.Text = "";
        }

        protected void btnSubmitRequisition_Click(object sender, EventArgs e)
        {
            if (txtDescription.Text == "Need to change Description")
            {
                lblDescriptionError.Text = "Please change the description before submitting request.";
                lblAstrisk.Visible = true;
            }
            else if (txtDescription.Text == "")
            {
                lblDescriptionError.Text = "Please enter a description before submitting request.";
                lblAstrisk.Visible = true;
            }
            else
            {
                presentation.ChangeRequisitionDescription(Convert.ToInt32(lblRequisitionID.Text), txtDescription.Text);
                presentation.SubmitRequisition(Convert.ToInt32(lblRequisitionID.Text));
                mvRequest.ActiveViewIndex = 0;
                Response.Redirect("RequisitionStoreManager.aspx");
            }
        }

        //Modified by Adam Venezia 10-21-12. Wrong call to delete a requisition item is fixed,
        //the stored procedure behind the CancelRequisition call now exists and works.
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            //Check first if there are items in a request to delete
            if (gvRequisition.Rows.Count > 0)
            {
                for (int i = 1; i <= gvRequisition.Rows.Count; i++)
                {
                    int index = i - 1;
                    GridViewRow row = gvRequisition.Rows[index];
                    presentation.DeleteRequisitionItem(Convert.ToInt32(lblRequisitionID.Text), Convert.ToInt32(((Label)row.FindControl("lblItemID")).Text),
                        Convert.ToInt32(((Label)row.FindControl("lblStoreID")).Text));
                }
            }
            //Then delete the request and redirect.
            presentation.CancelRequisition(Convert.ToInt32(lblRequisitionID.Text));
            Response.Redirect("RequisitionStoreManager.aspx");
        }

        protected void btnReallocate_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(lblTransferFromQty.Text) - Convert.ToInt32(txtTransferQty.Text) < 0)
            {
                lblReallocateError.Text = "Not enough in stock";
            }
            else if (Convert.ToInt32(txtTransferQty.Text) < 1)
            {
                lblReallocateError.Text = "Must be a positive Number";
            }
            else
            {
                lblReallocateError.Text = "";
                presentation.ReallocateItem(Convert.ToInt32(lblItemID.Text), GetStoreID(),
                    Convert.ToInt32(presentation.GetSubUnitByName(ddlTransferFromStore.SelectedItem.ToString()).Tables[1].Rows[0][0].ToString()),
                    Convert.ToInt32(presentation.GetSubUnitByName(lblStoreName.Text).Tables[1].Rows[0][0].ToString()),
                     Convert.ToInt32(txtTransferQty.Text));
                ds = presentation.GetStoreUnitItemQty(GetStoreID(), Convert.ToInt32(lblStoreNumber.Text), Convert.ToInt32(lblItemID.Text));

                txtTransferQty.Text = "";

                if (ds.Tables[0].Rows[0][0].ToString() == "")
                {
                    lblStoreQty.Text = ds.Tables[1].Rows[0][0].ToString();
                }
                else
                {
                    lblStoreQty.Text = ds.Tables[0].Rows[0][0].ToString();
                }

                ds = presentation.GetStoreUnitItemQty(GetStoreID(), Convert.ToInt32(ddlTransferFromStore.SelectedValue), Convert.ToInt32(lblItemID.Text));

                if (ds.Tables[0].Rows[0][0].ToString() == "")
                {
                    lblTransferFromQty.Text = ds.Tables[1].Rows[0][0].ToString();
                }
                else
                {
                    lblTransferFromQty.Text = ds.Tables[0].Rows[0][0].ToString();
                }
                Response.Redirect("~/RequisitionStoreManager.aspx");
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Session.Remove("Itemadd");
            mvViewRequest.ActiveViewIndex = 1;
        }

        protected void btnCreate2_Click(object sender, EventArgs e)
        {
            ds = presentation.AddRequisition(Convert.ToInt32(GetStoreID()), txtDescription2.Text);
            txtDescription.Text = "";
            IA = ItemAdd();
            ds = presentation.AddItemToRequisition(Convert.ToInt32(ds.Tables[1].Rows[0][0].ToString()), IA.ItemID, IA.Qty, IA.StoreID);
            Session.Remove("Itemadd");
            mvViewRequest.ActiveViewIndex = 1;

            lblViewRequestError.Text = ds.Tables[0].Rows[0][0].ToString();
        }

        protected void btnReceivedAdviceLog_Click(object sender, EventArgs e)
        {
            PassRequisitionID();
            Response.Redirect("~/ReceivedLog.aspx");
        }

        protected void btnYesApproval_Click(object sender, EventArgs e)
        {
            gvRequestItems.Enabled = true;
            btnYesApproval.Visible = false;
            btnNoApproval.Visible = false;
            markReady();
        }

        protected void btnNoApproval_Click(object sender, EventArgs e)
        {
            ((TextBox)gvRequestItems.Rows[IR.row].FindControl("txtRequestAdd")).Text = "";
            Session.Remove("Itemready");
            lblViewRequestError.Text = "";
            btnYesApproval.Visible = false;
            btnNoApproval.Visible = false;
            gvRequestItems.Enabled = true;
        }

        #endregion

        #region Session Control
        //Create and retrive session
        private Application_Classes.ItemSearch GetItemName()
        {
            if (Session["ItemName"] == null)
                Session.Add("ItemName", itemSearch);

            return (Application_Classes.ItemSearch)Session["ItemName"];
        }

        private Application_Classes.ItemAdd ItemAdd()
        {
            if (Session["Itemadd"] == null)
                Session.Add("Itemadd", IA);

            return (Application_Classes.ItemAdd)Session["Itemadd"];
        }

        private Application_Classes.ItemReady ItemReady()
        {
            if (Session["Itemready"] == null)
                Session.Add("Itemready", IR);

            return (Application_Classes.ItemReady)Session["Itemready"];
        }

        private DataSet ItemList()
        {
            if (Session["Items"] == null)
                Session.Add("Items", items);
            return (DataSet)Session["Items"];
        }

        private DataSet ItemAdjuster()
        {
            if (Session["ItemAdjuster"] == null)
                Session.Add("ItemAdjuster", itemAdjustmenter);
            return (DataSet)Session["ItemAdjuster"];
        }

        private DataSet ItemAllocating()
        {
            if (Session["ItemAllocating"] == null)
                Session.Add("ItemAllocating", itemAllocation);
            return (DataSet)Session["ItemAllocating"];
        }

        private int OpenReq()
        {
            if (Session["Req"] == null)
                Session.Add("Req", req);
            return (int)Session["Req"];
        }

        private int OpenProc()
        {
            if (Session["Proc"] == null)
                Session.Add("Proc", proc);
            return (int)Session["Proc"];
        }

        private int PassRequisitionID()
        {
            //if (Session["RequisitionID"] == null)
            Session.Add("RequisitionID", Convert.ToInt32(lblViewRequisitionID.Text));
            return (int)Session["RequisitionID"];
        }

        private int GetStoreID()
        {
            try
            {
                User lg = (User)Session["LoggedInUser"];
                return lg.getStoreId();
            }
            catch (Exception)
            {
                return 0;
            }
        }

        private int GetUserID()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getUserId();
        }

        private string GetFirstName()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getFirstName();
        }

        private string GetLastName()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getLastName();
        }
        private string GetSessionID()
        {
            try
            {
                User lg = (User)Session["LoggedInUser"];
                return lg.getSessionId();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                return null;
            }
        }
        private bool GetIsAdmin()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsAdmin();
        }
        private bool GetIsGeneralManager()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsGeneralManager();
        }
        private bool GetIsStoreManager()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsStoreManager();
        }
        private bool GetIsStoreKeeper()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsStoreKeeper();
        }
        private bool GetIsSubUnitClerk()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsSubUnitClerk();
        }
        private bool GetIsAccounting()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsAccounting();
        }
        private bool GetIsProcurement()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsProcurement();
        }
        private bool GetIsCEO()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsCEO();
        }

        #endregion Session Control

        #region GridviewControls

        #region gvItemSearch Controls
        protected void populateItemGridView(DataSet i)
        {
            if (i.Tables[0].Rows[0][0].ToString() != "")
            {
                string me = ds.Tables[0].Rows[0][0].ToString();
                lblMyError.Text = me;

                gvItemSearch.DataSource = null;
                gvItemSearch.DataBind();
            }
            else
            {
                gvItemSearch.DataSource = i.Tables[1];
                gvItemSearch.DataBind();

                if (gvItemSearch.PageIndex == gvItemSearch.PageCount - 1)
                {
                    ((LinkButton)gvItemSearch.BottomPagerRow.FindControl("lnkNextPage")).Enabled = false;
                }
                else
                {
                    ((LinkButton)gvItemSearch.BottomPagerRow.FindControl("lnkNextPage")).Enabled = true;
                }


                if (gvItemSearch.PageIndex == 0)
                {
                    ((LinkButton)gvItemSearch.BottomPagerRow.FindControl("lnkPrevPage")).Enabled = false;
                }
                else
                {
                    ((LinkButton)gvItemSearch.BottomPagerRow.FindControl("lnkPrevPage")).Enabled = true;  
                }
                lblMyError.Text = "";
            }

        }
        protected void gvItemSearch_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            items = ItemList();
            gvItemSearch.PageIndex = e.NewPageIndex;
            gvItemSearch.DataBind();
            populateItemGridView(items);
        }
        protected void gvItemSearch_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Add")
            {
                // Retrieve the row index stored in the 
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);


                // Retrieve the row that contains the button 
                // from the Rows collection.
                GridViewRow row = gvItemSearch.Rows[index];
                int Num;
                bool isNum = int.TryParse(((TextBox)row.FindControl("txtQuantity")).Text, out Num);
                if (!isNum)
                {
                    lblMyError.Text = "Enter a number";
                }
                else if (Convert.ToInt32(((TextBox)row.FindControl("txtQuantity")).Text) < 1)
                {
                    lblMyError.Text = "Enter amount greater then 0";
                }
                else if (((TextBox)row.FindControl("txtQuantity")).Text == "")
                {
                    lblMyError.Text = "Must enter a valid quantity";
                }

                else
                {
                    lblMyError.Text = "";
                    //the drop down list ddlSubUnit, or the "Order For" field is used in DB code here.
                    presentation.AddItemToRequisition(Convert.ToInt32(lblRequisitionID.Text), Convert.ToInt32((row.FindControl("lblItemID")as Label).Text), Convert.ToInt32(((TextBox)row.FindControl("txtQuantity")).Text), 1);

                    ((TextBox)row.FindControl("txtQuantity")).Text = "";

                    btnSubmitRequisition.Visible = true;
                    lblRequestMissing.Text = "";
                    ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(lblRequisitionID.Text), false);
                    gvRequisition.DataSource = ds.Tables[1];
                    gvRequisition.DataBind();
                }
            }
        }
        #endregion

        #region gvViewRequisition

        protected void gvViewRequisition_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                // Retrieve the row index stored in the 
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);


                // Retrieve the row that contains the button 
                // from the Rows collection.
                GridViewRow row = gvViewRequisition.Rows[index];
                ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(gvViewRequisition.Rows[index].Cells[0].Text), true);
                if (ds.Tables[0].Rows[0][0].ToString() == "")
                {
                    // Fill in the requisition info
                    gvViewRequisitionItems.DataSource = ds.Tables[1];
                    lblViewRequisitionID.Text = row.Cells[0].Text;
                    lblViewRequisitionDate.Text = row.Cells[2].Text;
                    lblViewDescription.Text = row.Cells[1].Text;
                    DataSet reqInfo = presentation.GetRequisitionByRequisitionID(Convert.ToInt32(row.Cells[0].Text));
                    String comments = reqInfo.Tables[1].Rows[0]["DenialComments"].ToString();
                    lblComments.Text = comments;

                    // Set status and color of status label for the requisition as a whole
                    Label lblStatusName = row.FindControl("lblStatusName") as Label;
                    String status = lblStatusName.Text;
                    lblViewStatus.Text = status;
                    if (status == "Pending")
                    {
                        lblViewStatus.ForeColor = Color.DarkOrange;
                        btnHideRequisition.Visible = false;
                    }
                    else if (status.Contains("Approved"))
                    {
                        lblViewStatus.ForeColor = Color.DarkGreen;
                        btnHideRequisition.Visible = false;
                    }
                    else if (status == "Closed")
                    {
                        lblViewStatus.ForeColor = Color.Red;
                        btnHideRequisition.Visible = true;
                    }

                    if (status == "Ordered")
                    {
                        btnReceivedAdviceLog.Visible = true;
                    }
                    else
                    {
                        btnReceivedAdviceLog.Visible = false;
                    }
                }
                else
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();

                gvViewRequisitionItems.DataBind();

                // Set status and color of status label for each item in the requisition
                foreach (GridViewRow itemRow in gvViewRequisitionItems.Rows)
                {
                    Label lblItemStatus = itemRow.FindControl("lblItemStatus") as Label;
                    String itemStatus = lblItemStatus.Text;
                    if (itemStatus == "Pending")
                    {
                        lblItemStatus.ForeColor = Color.DarkOrange;
                    }
                    else if (itemStatus == "Ordered")
                    {
                        lblItemStatus.ForeColor = Color.DarkGreen;
                    }
                    else if (itemStatus == "Closed")
                    {
                        lblItemStatus.ForeColor = Color.Red;
                    }
                }

                ds = presentation.CheckSignature(Convert.ToInt32(lblViewRequisitionID.Text));
                if (ds.Tables[0].Rows[0][0].ToString() == "")
                {
                    lblGMSigned.Text = ds.Tables[1].Rows[0][0].ToString();
                    lblCEOSigned.Text = ds.Tables[1].Rows[0][1].ToString();

                    PassRequisitionID();

                    //if (lblCEOSigned.Text == "Waiting Approval")
                    //    btnCEOReceipt.Style.Add("visibility", "visible");
                    //else
                    //    btnCEOReceipt.Style.Add("visibility", "hidden");
                }
                else
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();

                mvViewRequisition.ActiveViewIndex = 1;

            }
        }

        #endregion

        #region gvViewRequest
        protected void gvRequests_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                // Retrieve the row index stored in the 
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);


                // Retrieve the row that contains the button 
                // from the Rows collection.
                GridViewRow row = gvRequests.Rows[index];

                ds = presentation.GetRequestItemsByRequestByStore(Convert.ToInt32(row.Cells[0].Text), GetStoreID());

                if (ds.Tables[0].Rows[0][0].ToString() == "") //no errors, bind table
                    gvRequestItems.DataSource = ds.Tables[1];
                else
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();

                gvRequestItems.DataBind();

                // Color each item's status
                foreach (GridViewRow gvr in gvRequestItems.Rows)
                {
                    Label lblStatusName = gvr.FindControl("lblStatusName") as Label;
                    String status = lblStatusName.Text;
                    if (status == "Pending")
                    {
                        lblStatusName.ForeColor = Color.DarkOrange;
                        lblStatusName.BackColor = Color.PeachPuff; 
                    }
                    if (status == "Ready") { lblStatusName.ForeColor = Color.DarkGreen; }
                }

                lblViewRequestDate.Text = row.Cells[3].Text;
                lblViewRequestID.Text = row.Cells[0].Text;
                lblViewRequestDescription.Text = row.Cells[1].Text;
                //lblStoreID.Text = row.Cells[2].Text;
                lblViewRequestError.Text = "";

                // ds = presentation.GetStoreByID(Convert.ToInt32(lblStoreID.Text));

                lblRequestStoreName.Text = row.Cells[2].Text;

                mvViewRequest.ActiveViewIndex = 1;
            }
        }
        #endregion

        #region gvRequestFill
        protected void gvRequestFill_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Choose")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvRequestFill.Rows[index];

                lblFillRequestDate.Text = row.Cells[3].Text;
                lblFillRequestID.Text = row.Cells[0].Text;
                lblFillRequestDescription.Text = row.Cells[1].Text;

                ds = presentation.ItemsandQuantity(Convert.ToInt32(lblFillRequestID.Text), GetStoreID());
                if (ds.Tables[0].Rows[0][0].ToString() == "")
                {
                    gvRequestFillItems.DataSource = ds.Tables[1];


                    foreach (GridViewRow gvr in gvRequestFillItems.Rows)
                    {
                        Label lblApproved = gvr.FindControl("lblApproved") as Label;
                        String requested = gvr.Cells[2].Text;
                        String approved = lblApproved.Text;
                        if (requested != approved)
                            lblApproved.ForeColor = Color.Orange;
                    }
                }
                else
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();

                gvRequestFillItems.DataBind();

                mvRequestFill.ActiveViewIndex = 1;
            }
        }

        protected void gvRequestFillItems_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Fill")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvRequestFillItems.Rows[index];
                int approvedAmt = Convert.ToInt32((row.FindControl("lblApproved") as Label).Text);

                int Num;
                bool isNum = int.TryParse(((TextBox)row.FindControl("txtFill")).Text, out Num);
                if (!isNum)
                {
                    lblFillRequestError.Text = "Please enter a number";
                }
                else if (Num < 1)
                {
                    lblFillRequestError.Text = "Enter amount greater then 0";
                }
                else if (((TextBox)row.FindControl("txtFill")).Text == "")
                {
                    lblFillRequestError.Text = "Must enter a valid quantity";
                }
                // Added extra validation to check if the entered amount is more than the allocated/approved amount 
                // or the entered amount is more than the allocated amount by itself
                else if (Convert.ToInt32(row.Cells[5].Text) + Num > approvedAmt || Num > approvedAmt)
                {
                    lblFillRequestError.Text = "Cannot receive more than Approved";
                }
                else
                {
                    lblFillRequestError.Text = "";// Added to refresh the label
                    int qty = Convert.ToInt32(((TextBox)row.FindControl("txtFill")).Text);
                    int requestID = Convert.ToInt32(lblFillRequestID.Text);
                    int itemID = Convert.ToInt32((row.FindControl("lblItemID") as Label).Text);
                    // Need to receive the amount allocated/approved
                    presentation.FillRequestItem(requestID, itemID, qty, GetStoreID());
                    ((TextBox)row.FindControl("txtFill")).Text = "";

                    if (approvedAmt != qty && qty != 0) // If the updateQty is not equal to the ApprovedQty and the updateQty is not zero
                    {
                        presentation.MarkRequestItemPartialFill(requestID, itemID); // Change the Request Item Status to Partially Filled
                    }
                    ds = presentation.ItemsandQuantity(Convert.ToInt32(lblFillRequestID.Text), GetStoreID());
                    gvRequestFillItems.DataSource = ds.Tables[1];
                    gvRequestFillItems.DataBind();
                }

            }

        }
        protected void gvRequestFillItems_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow &&
                (e.Row.RowState & DataControlRowState.Normal) == DataControlRowState.Normal)
            {
                if (Convert.ToInt32((e.Row.FindControl("lblApproved") as Label).Text) == Convert.ToInt32(e.Row.Cells[5].Text))
                {
                    ((TextBox)e.Row.FindControl("txtFill")).Enabled = false;
                    e.Row.Cells[4].Enabled = false;
                    presentation.MarkRequestItemClosed(Convert.ToInt32(lblFillRequestID.Text), Convert.ToInt32((e.Row.FindControl("lblItemID") as Label).Text));
                }
            }
        }
        #endregion

        #region gvRequestItems
        protected void gvRequestItems_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Ready")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvRequestItems.Rows[index];

                if (Convert.ToInt32(((TextBox)row.FindControl("txtRequestAdd")).Text) > Convert.ToInt32(row.Cells[1].Text))
                {
                    lblViewRequestError.Text = "Cannot Approve more than requested";
                }
                else
                {
                    //are we requesting more than is in stock?
                    if (Convert.ToInt32(((TextBox)row.FindControl("txtRequestAdd")).Text) <= Convert.ToInt32(row.Cells[3].Text))
                    {
                        int requestAdd = Convert.ToInt32(((TextBox)row.FindControl("txtRequestAdd")).Text);
                        int requested = Convert.ToInt32(row.Cells[1].Text);
                        int approved = Convert.ToInt32(((Label)row.FindControl("lblAmtApproved")).Text);

                        if ((requested - approved) >= requestAdd)
                        {
                            //we are not requesting more than is in stock; are we requesting enough to dip into emergency quantity?
                            //remember, emergency quantity is a part of the total quantity. We know we're trying to dip into emergency quantity if
                            //what is requested is greater than the total quantity minus how many are meant to be emergency quantity.
                            if (Convert.ToInt32(((TextBox)row.FindControl("txtRequestAdd")).Text) > Convert.ToInt32(row.Cells[3].Text) - Convert.ToInt32(row.Cells[2].Text))
                            {
                                //yes trying to dip into emergency quantity
                                IR.RequestID = Convert.ToInt32(lblViewRequestID.Text);
                                IR.ItemID = Convert.ToInt32((row.FindControl("lblItemID") as Label).Text);
                                IR.Qty = requestAdd;
                                IR.row = index;
                                IR.available = Convert.ToInt32(row.Cells[3].Text);
                                IR.emergency = Convert.ToInt32(row.Cells[2].Text);
                                IR.RequestItemID = Convert.ToInt32((row.FindControl("lblRequestItemID") as Label).Text);
                                ItemReady(); //Create ItemReady Session if doesn't exist
                                lblViewRequestError.Text = "Do you have manager approval for reducing emergency amount?";
                                btnYesApproval.Visible = true;
                                btnNoApproval.Visible = true;
                                gvRequestItems.Enabled = false;
                            }
                            else
                            {
                                //no, there is enough stock to not dip into emergency quantity.
                                IR.RequestID = Convert.ToInt32(lblViewRequestID.Text);
                                IR.ItemID = Convert.ToInt32((row.FindControl("lblItemID") as Label).Text);
                                IR.Qty = Convert.ToInt32(((TextBox)row.FindControl("txtRequestAdd")).Text);
                                IR.row = index;
                                IR.available = Convert.ToInt32(row.Cells[3].Text);
                                IR.emergency = Convert.ToInt32(row.Cells[2].Text);
                                IR.RequestItemID = Convert.ToInt32((row.FindControl("lblRequestItemID") as Label).Text);
                                ItemReady(); //Create ItemReady Session if it doesn't exist
                                markReady();
                            }
                        }
                        else
                            lblViewRequestError.Text = "Cannot approve more than was requested.";
                    }
                        //we requested more than is in stock
                    else
                    {
                        lblViewRequestError.Text = "Not enough in stock to make ready";
                    }
                }

            }
        }

        protected void markReady()
        {
            IR = ItemReady();
            //int qtyApproved = IR.available + IR.emergency - IR.Qty;
            //if (qtyApproved < IR.emergency) // Updates the Emergency Quantity
            //{
            //    presentation.UpdateEmergencyQuantity(IR.ItemID, qtyApproved);
            //}
            //Changed to add the Allowed (Allocated) amount as well as set the status of the Item
            presentation.MarkItemReady(IR.RequestID, IR.ItemID, IR.Qty, IR.RequestItemID); // Marks the Item Status to Ready for Pickup
            presentation.UpdateRequestStatus(IR.RequestID); // Updates Request Status
            ds = presentation.GetRequestItemsByRequestByStore(Convert.ToInt32(lblViewRequestID.Text), GetStoreID());
            ((TextBox)gvRequestItems.Rows[IR.row].FindControl("txtRequestAdd")).Text = "";
            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                gvRequestItems.DataSource = ds.Tables[1];
                lblViewRequestError.Text = "Items ready to be filled";
                Session.Remove("Itemready");
            }
            else
                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();

            gvRequestItems.DataBind();

            // Color each item's status
            foreach (GridViewRow gvr in gvRequestItems.Rows)
            {
                Label lblStatusName = gvr.FindControl("lblStatusName") as Label;
                String status = lblStatusName.Text;
                if (status == "Pending")
                {
                    lblStatusName.ForeColor = Color.DarkOrange;
                    lblStatusName.BackColor = Color.PeachPuff; 
                }
                if (status == "Ready") { lblStatusName.ForeColor = Color.DarkGreen; }
            }
			
			// If new quantity for this item is less than the minimum stock quantity, show an alert telling the user to order more
            DataSet itemInfo = presentation.GetModifyInfoByID(IR.ItemID);	// For the minStockQty and itemName
            int minStockQty = Convert.ToInt32(itemInfo.Tables[1].Rows[0]["minStockQty"]);
            if ((IR.available - IR.Qty) <= minStockQty)
			{
				// Register an alert to tell the user to requisition more of this item
                String itemName = itemInfo.Tables[1].Rows[0]["itemName"].ToString();
                String alertMsg = "You are running low on \"" + itemName + "\"\\nRequisition more of this item to avoid running out of stock.";
				String alertTitle = "Low stock";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), alertMsg, "alert('" + alertTitle + "\\n\\n" + alertMsg + "')", true);
			}
        }
        #endregion

        //Modified by Adam Venezia 10-15-12, removed StoreID as we now display the store name, rather than its ID
        protected void gvRequisition_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Remove")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvRequisition.Rows[index];

                presentation.DeleteRequisitionItem(Convert.ToInt32(lblRequisitionID.Text), Convert.ToInt32((row.FindControl("lblItemID") as Label).Text), Convert.ToInt32(((Label)row.FindControl("lblStoreID")).Text));

                ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(lblRequisitionID.Text), false);

                if (ds.Tables[0].Rows[0][0].ToString() == "")
                {
                    gvRequisition.DataSource = ds.Tables[1];
                    gvRequisition.DataBind();
                    btnSubmitRequisition.Visible = true;
                    lblRequestMissing.Text = "";
                }
                else
                {
                    gvRequisition.DataSource = null;
                    gvRequisition.DataBind();
                    btnSubmitRequisition.Visible = false;
                    lblRequestMissing.Text = "There are currently no items for this request";
                }
            }
        }

        protected void ddlTransferFromStore_SelectedIndexChanged(object sender, EventArgs e)
        {
            ds = presentation.GetStoreUnitItemQty(GetStoreID(), Convert.ToInt32(ddlTransferFromStore.SelectedValue), Convert.ToInt32(lblItemID.Text));

            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                lblTransferFromQty.Text = ds.Tables[1].Rows[0][0].ToString();
            }
            else
            {
                lblTransferFromQty.Text = ds.Tables[0].Rows[0][0].ToString();
            }
        }

        protected void populateItemAdjustments(DataSet i)
        {
            if (i.Tables[0].Rows[0][0].ToString() != "")
            {
                string me = ds.Tables[0].Rows[0][0].ToString();
                lblMasterError.Text = me;

                gvItemAdjustment.DataSource = null;
                gvItemAdjustment.DataBind();
            }
            else
            {
                gvItemAdjustment.DataSource = i.Tables[1];
                gvItemAdjustment.DataBind();


                if (gvItemAdjustment.Rows.Count > 0)
                {
                    if (gvItemAdjustment.PageIndex == gvItemAdjustment.PageCount - 1)
                    {
                        ((LinkButton)gvItemAdjustment.BottomPagerRow.FindControl("lnkNextPage")).Enabled = false;
                    }
                    else
                    {
                        ((LinkButton)gvItemAdjustment.BottomPagerRow.FindControl("lnkNextPage")).Enabled = true;
                    }


                    if (gvItemAdjustment.PageIndex == 0)
                    {
                        ((LinkButton)gvItemAdjustment.BottomPagerRow.FindControl("lnkPrevPage")).Enabled = false;
                    }
                    else
                    {
                        ((LinkButton)gvItemAdjustment.BottomPagerRow.FindControl("lnkPrevPage")).Enabled = true;
                    }
                }
            }

        }

        protected void gvItemAdjustment_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            itemAdjustmenter = ItemAdjuster();
            gvItemAdjustment.PageIndex = e.NewPageIndex;
            gvItemAdjustment.DataBind();
            populateItemAdjustments(itemAdjustmenter);
        }

        protected void gvItemAdjustment_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow &&
                (e.Row.RowState & DataControlRowState.Normal) == DataControlRowState.Normal)
            {
                ds = presentation.GetItemAdjustmentReasons();
                ((DropDownList)e.Row.FindControl("ddlAdjusmentReason")).DataSource = ds.Tables[1];
                ((DropDownList)e.Row.FindControl("ddlAdjusmentReason")).DataTextField = "AdjName";
                ((DropDownList)e.Row.FindControl("ddlAdjusmentReason")).DataValueField = "AdjReasonID";
                ((DropDownList)e.Row.FindControl("ddlAdjusmentReason")).DataBind();

            }
        }

        protected void gvRequestItems_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow &&
                (e.Row.RowState & DataControlRowState.Normal) == DataControlRowState.Normal)
            {
                if (e.Row.Cells[5].Text == "Ready" || e.Row.Cells[5].Text == "Picked-Up" || e.Row.Cells[5].Text == "Partially Picked-Up")
                {
                    ((TextBox)e.Row.FindControl("txtRequestAdd")).Enabled = false;
                    e.Row.Cells[7].Enabled = false;
                    e.Row.Cells[8].Enabled = false;
                    e.Row.Cells[9].Enabled = false;

                }

            }
        }

        #endregion

        protected void gvItemAdjustment_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Adjust")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvItemAdjustment.Rows[index];

                int Num;
                bool isNum = int.TryParse(((TextBox)row.FindControl("txtAddQuantity")).Text, out Num);
                if (!isNum)
                {
                    lblErrorItemAdjustment.Text = "Please enter a number";
                }
                if (Num <= 0)
                {
                    lblErrorItemAdjustment.Text = "Please enter a number greater than 0";
                }
                else if (((TextBox)row.FindControl("txtAddQuantity")).Text == "")
                {
                    lblErrorItemAdjustment.Text = "Must enter a valid quantity";
                }
                else
                {
                    int itemID = Convert.ToInt32((row.FindControl("lblItemID") as Label).Text);
                    int quantity = -(Convert.ToInt32(((TextBox)row.FindControl("txtAddQuantity")).Text));    // Take the negative amount because we want to remove this quantity from the current amount
                    int reasonID = Convert.ToInt32(((DropDownList)row.FindControl("ddlAdjusmentReason")).SelectedValue);
                    int userID = GetUserID();
                    int storeID = GetStoreID();
                    int subUnitID = 1;

                    // Check to see if taking away the amount entered is a valid thing to do
                    //int i = Convert.ToInt32(e.CommandArgument);
                    String itemName = (row.FindControl("lblItemName") as Label).Text;
                    ds = presentation.GetItemByName(itemName);
                    if (ds.Tables[0].Rows[0][0].ToString() == "")
                    {
                        int totalQty = Convert.ToInt32(ds.Tables[1].Rows[0]["TotalQuantity"].ToString());
                        if (-quantity > totalQty)
                        {
                            lblErrorItemAdjustment.Text = "Quantity entered must be less than the current quantity";
                            return;
                        }


                        ds = presentation.AdjustItemQuantity(
                        itemID,
                        quantity,
                        reasonID,
                        userID,
                        storeID,
                        subUnitID);

                        if (ds.Tables[0].Rows[0][0].ToString() != "")
                            lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();

                        ds = presentation.GetAllItemsForStoreID(GetStoreID(), 1);
                        if (ds.Tables[0].Rows[0][0].ToString() == "")
                        {
                            Session.Remove("ItemAdjuster");
                            itemAdjustmenter = ds;
                            ItemAdjuster();
                            populateItemAdjustments(itemAdjustmenter);
                            lblErrorItemAdjustment.Text = "";
                        }
                        else
                            lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                    }
                    else
                        lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                }
            }
        }

        #region Allocation Methods
        //protected void gvAllocation_RowCommand(object sender, GridViewCommandEventArgs e)
        //{
        //    if (e.CommandName == "Fill")
        //    {
        //        int index = Convert.ToInt32(e.CommandArgument);
        //        GridViewRow row = gvAllocation.Rows[index];
        //        ((TextBox)row.FindControl("txtAllocateEmergency")).Text = "";

        //        int Num;
        //        bool isNum = int.TryParse(((TextBox)row.FindControl("txtFill")).Text, out Num);
        //        if (!isNum)
        //        {
        //            lblAllocationTabError.Text = "Please enter a number";
        //        }
        //        if (Num <= 0)
        //        {
        //            lblAllocationTabError.Text = "Please enter a number greater than zero";
        //        }
        //        else if (((TextBox)row.FindControl("txtFill")).Text == "")
        //        {
        //            lblAllocationTabError.Text = "Must enter a valid quantity";
        //        }


        //        else
        //        {
        //            presentation.AllocateItem(GetStoreID(), 0, Convert.ToInt32(row.Cells[0].Text), Convert.ToInt32(((TextBox)row.FindControl("txtFill")).Text));

        //            ((TextBox)row.FindControl("txtFill")).Text = "";

        //            ds = presentation.GetAllItemsForStoreID(GetStoreID(), 1);
        //            Session.Remove("ItemAllocating");
        //            itemAllocation = ds;
        //            ItemAllocating();
        //            populateGridviewByName(itemAllocation);
        //        }
        //    }

        //    if (e.CommandName == "AllEmerg")
        //    {
        //        int index = Convert.ToInt32(e.CommandArgument);
        //        GridViewRow row = gvAllocation.Rows[index];
        //        ((TextBox)row.FindControl("txtFill")).Text = "";

        //        int Num;
        //        bool isNum = int.TryParse(((TextBox)row.FindControl("txtAllocateEmergency")).Text, out Num);
        //        if (!isNum)
        //        {
        //            lblAllocationTabError.Text = "Please enter a number";
        //        }
        //        if (Num <= 0)
        //        {
        //            lblAllocationTabError.Text = "Please enter a number greater than zero";
        //        }
        //        else if (((TextBox)row.FindControl("txtAllocateEmergency")).Text == "")
        //        {
        //            lblAllocationTabError.Text = "Must enter a valid quantity";
        //        }

        //        else
        //        {
        //            presentation.AllocateItem(GetStoreID(), Convert.ToInt32(((TextBox)row.FindControl("txtAllocateEmergency")).Text), Convert.ToInt32(row.Cells[0].Text), 0);

        //            ((TextBox)row.FindControl("txtAllocateEmergency")).Text = "";

        //            ds = presentation.GetAllItemsForStoreID(GetStoreID(), 1);
        //            Session.Remove("ItemAllocating");
        //            itemAllocation = ds;
        //            ItemAllocating();
        //            populateGridviewByName(itemAllocation);
        //        }
        //    }
        //}
        #endregion

        #region Allocation Gridview Controls
        protected void populateGridviewByName(DataSet i)
        {

            if (i.Tables[0].Rows[0][0].ToString() != "")
            {
                string me = ds.Tables[0].Rows[0][0].ToString();
                lblMasterError.Text = me;

                gvAllocation.DataSource = null;
                gvAllocation.DataBind();
            }
            else
            {
                gvAllocation.DataSource = i.Tables[1];
                gvAllocation.DataBind();


                if (gvAllocation.Rows.Count > 0)
                {
                    if (gvAllocation.PageIndex == gvAllocation.PageCount - 1)
                    {
                        ((LinkButton)gvAllocation.BottomPagerRow.FindControl("lnkNextPage")).Enabled = false;
                    }
                    else
                    {
                        ((LinkButton)gvAllocation.BottomPagerRow.FindControl("lnkNextPage")).Enabled = true;
                    }


                    if (gvAllocation.PageIndex == 0)
                    {
                        ((LinkButton)gvAllocation.BottomPagerRow.FindControl("lnkPrevPage")).Enabled = false;
                    }
                    else
                    {
                        ((LinkButton)gvAllocation.BottomPagerRow.FindControl("lnkPrevPage")).Enabled = true;
                    }
                }
            }
        }
        #endregion

        protected void gvAllocation_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            itemAllocation = ItemAllocating();
            gvAllocation.PageIndex = e.NewPageIndex;
            populateGridviewByName(itemAllocation);
        }

        

        protected void gvRequisition_RowEditing(object sender, GridViewEditEventArgs e)
        {
            ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(lblRequisitionID.Text), false);
            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                gvRequisition.DataSource = ds.Tables[1];
                gvRequisition.EditIndex = e.NewEditIndex;
                gvRequisition.DataBind();
                //GridViewRow row = gvRequisition.Rows[e.NewEditIndex];
                gvRequisition.Columns[3].Visible = false;
            }
            else
            {
                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                gvRequisition.DataBind();
            }
        }

        protected void gvRequisition_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvRequisition.EditIndex = -1;
            ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(lblRequisitionID.Text), false);
            if (ds.Tables[0].Rows[0][0].ToString() == "")
                gvRequisition.DataSource = ds.Tables[1];
            else
                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
            gvRequisition.DataBind();
            gvRequisition.Columns[3].Visible = true;
            //GridViewRow row = gvRequisition.Rows[e.RowIndex];
            //row.Cells[5].Enabled = true;
        }

        protected void gvRequisition_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = gvRequisition.Rows[e.RowIndex];
            int qty = Convert.ToInt32(((TextBox)row.FindControl("txtEditRequisitionQty")).Text);

            presentation.ModifyRequisition(Convert.ToInt32(lblRequisitionID.Text),
                Convert.ToInt32(((Label)row.FindControl("lblStoreID")).Text), Convert.ToInt32((row.FindControl("lblItemID") as Label).Text), qty);
            gvRequisition.EditIndex = -1;
            ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(lblRequisitionID.Text), false);
            if (ds.Tables[0].Rows[0][0].ToString() == "")
                gvRequisition.DataSource = ds.Tables[1];
            else
                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
            gvRequisition.DataBind();
            gvRequisition.Columns[3].Visible = true;
        }

        public String CalcCost(Object cost, Object qty)
        {
            //DataBind functions always pass in generic objects, not strongly typed objects
            Double dCost = Convert.ToDouble(cost);
            Double dQty = Convert.ToDouble(qty);

            return String.Format("{0:0.00}", (dCost * dQty));
        }

        private void populateInventoryTables()
        {
            // This will refresh the data in the the "Item Adjustment" and "Received Inventory" tabs
            ds = presentation.GetAllItemsForStoreID(GetStoreID(), 1);
            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                Session.Remove("ItemAllocating");
                itemAllocation = ds;
                ItemAllocating();
                populateGridviewByName(itemAllocation);

                Session.Remove("ItemAdjuster");
                itemAdjustmenter = ds;
                ItemAdjuster();
                populateItemAdjustments(itemAdjustmenter);

                lblAllocationTabError.Text = lblErrorItemAdjustment.Text = "";
            }
            else
                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();

            // If we have a request id to work with, then refresh the "View Requests" tab table
            if (lblViewRequestID.Text.Length > 0)
            {
                ds = presentation.GetRequestItemsByRequestByStore(Convert.ToInt32(lblViewRequestID.Text), GetStoreID());
                gvRequestItems.DataSource = ds.Tables[1];
                gvRequestItems.DataBind();

                // Color each item's status
                foreach (GridViewRow gvr in gvRequestItems.Rows)
                {
                    Label lblStatusName = gvr.FindControl("lblStatusName") as Label;
                    String status = lblStatusName.Text;
                    if (status == "Pending")
                    {
                        lblStatusName.ForeColor = Color.DarkOrange;
                        lblStatusName.BackColor = Color.PeachPuff; 
                    }
                    if (status == "Ready") { lblStatusName.ForeColor = Color.DarkGreen; }
                }
            }
        }

        protected void TabContainer1_ActiveTabChanged(object sender, EventArgs e)
        {
            // Refill the item inventory tables
            populateInventoryTables();
        }

        //Created by Adam Venezia 10-21-12
        /// <summary>
        /// This button will be visible if CEO Signature field from DB = "Waiting Approval"
        /// This button will link to the page CEOSignOff.aspx, where session will carry over the requisition id.
        /// the information it needs to create a printer friendly report of the requisition to approve.
        /// </summary>
        //protected void btnCEOReceipt_Click(object sender, EventArgs e)
        //{
        //    PassRequisitionID();
        //    Response.Redirect("~/CEOSignOff.aspx");
        //}

        //Created by Adam Venezia, mainly copied from Jason 10-27-2012
        /// <summary>
        /// Gets session sort direction and then calls the method that creates the DataView that sorts
        /// </summary>
        protected void gvAllocation_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (AllocationSortDirection == SortDirection.Ascending)
            {
                AllocationSortDirection = SortDirection.Descending;
                AllcationSort(e.SortExpression, "DESC");
            }
            else
            {
                AllocationSortDirection = SortDirection.Ascending;
                AllcationSort(e.SortExpression, "ASC");
            }
        }

        //Created by Jason Vance 10-26-2012
        /// <summary>
        /// This will sort the item searching gridview
        /// </summary>
        protected void gvItemSearch_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                // Sort the data
                if (GridViewSortDirection == SortDirection.Ascending)
                {
                    GridViewSortDirection = SortDirection.Descending;
                    SortGridView(e.SortExpression, "DESC");
                }
                else
                {
                    GridViewSortDirection = SortDirection.Ascending;
                    SortGridView(e.SortExpression, "ASC");
                }
            }
            catch (Exception ex)
            {
            }
        }

        //Created by Jason Vance 10-26-2012
        /// <summary>
        /// This will keep track of the sort direction between postbacks
        /// </summary>
        public SortDirection GridViewSortDirection
        {
            get
            {
                if (Session["sortDirection"] == null)
                    Session["sortDirection"] = SortDirection.Ascending;

                return (SortDirection)Session["sortDirection"];
            }
            set { Session["sortDirection"] = value; }
        }

        //Created by Adam Venezia, mainly copied from Jason Vance, 10-27-12
        /// <summary>
        /// Keep track of Allocation's specific sort direction between posts.
        /// </summary>
        public SortDirection AllocationSortDirection
        {
            get
            {
                if (Session["allocationSortDirection"] == null)
                    Session["allocationSortDirection"] = SortDirection.Ascending;

                return (SortDirection)Session["allocationSortDirection"];
            }
            set { Session["allocationSortDirection"] = value; }
        }

        //Created by Jason Vance 10-26-2012
        /// <summary>
        /// This does the actual work of sorting the data that will be put into gvItemSearch
        /// </summary>
        private void SortGridView(string sortExpression, string direction)
        {
            try
            {
                // Get the current list of items
                ds = ItemList();
                DataTable dt = ds.Tables[1];

                // Sort that list
                DataView dv = new DataView(dt);
                dv.Sort = sortExpression + " " + direction;

                // Replace the current list with the new sorted one
                ds.Tables.RemoveAt(1);
                ds.Tables.Add(dv.ToTable());
                Session.Add("Items", ds);

                // Bind it to the gridview
                gvItemSearch.DataSource = dv;
                gvItemSearch.DataBind();
            }
            catch (Exception ex)
            {
            }
        }

        //Created by Adam Venezia, mainly copied from Jason 10-27-2012
        /// <summary>
        /// This does the actual work of sorting the data that will be put into gvItemSearch
        /// </summary>
        private void AllcationSort(String sortExpression, String direction)
        {
            itemAllocation = ItemAllocating();

            DataTable dt = itemAllocation.Tables[1];

            //Sorting with DataView
            DataView dv = new DataView(dt);
            dv.Sort = sortExpression + " " + direction;

            //replace original table into Dataset with sorted table
            itemAllocation.Tables.RemoveAt(1);
            itemAllocation.Tables.Add(dv.ToTable());

            //Call binding method
            populateGridviewByName(itemAllocation);
        }

        protected String GetAllocated(Object alloc)
        {
            String sAlloc = alloc.ToString();
            String rv = "0";

            if (sAlloc != "")
                rv = sAlloc;

            return rv;
        }

        protected void gvRequestItems_DataBound(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvRequestItems.Rows)
            {
                //lblAmtApproved
                //column 2
                int approved = Convert.ToInt32((row.FindControl("lblAmtApproved") as Label).Text);
                int requested = Convert.ToInt32(row.Cells[1].Text);
                Button btn = row.FindControl("btnApprove") as Button;

                if (approved == requested)
                    btn.Enabled = false;
            }
        }

        protected void btnOutsideCategorySearch_Click(object sender, EventArgs e)
        {
            gvOutsideItemSearch.EditIndex = -1;
            Session.Remove("ItemName");
            Session.Remove("Items");
            itemSearch.searchMethod = 1;
            itemSearch.categorySearched = Convert.ToInt32(ddlOutsideItemCategory.SelectedValue);
            GetItemName();
            ds = presentation.GetItemByCategoryByStore(itemSearch.categorySearched, GetStoreID());
            items = ds;
            ItemList();
            populateOutsideItemGridView(items);
        }

        protected void populateOutsideItemGridView(DataSet i)
        {
            if (i.Tables[0].Rows[0][0].ToString() != "")
            {
                string me = ds.Tables[0].Rows[0][0].ToString();
                lblOutsideMyError.Text = me;

                gvOutsideItemSearch.DataSource = null;
                gvOutsideItemSearch.DataBind();
            }
            else
            {
                gvOutsideItemSearch.DataSource = i.Tables[1];
                gvOutsideItemSearch.DataBind();

                if (gvOutsideItemSearch.PageIndex == gvItemSearch.PageCount - 1)
                {
                    ((LinkButton)gvOutsideItemSearch.BottomPagerRow.FindControl("lnkNextPage")).Enabled = false;
                }
                else
                {
                    ((LinkButton)gvOutsideItemSearch.BottomPagerRow.FindControl("lnkNextPage")).Enabled = true;
                }


                if (gvOutsideItemSearch.PageIndex == 0)
                {
                    ((LinkButton)gvOutsideItemSearch.BottomPagerRow.FindControl("lnkPrevPage")).Enabled = false;
                }
                else
                {
                    ((LinkButton)gvOutsideItemSearch.BottomPagerRow.FindControl("lnkPrevPage")).Enabled = true;
                }
                lblOutsideMyError.Text = "";
            }

        }

        protected void btnOutsideSearchItemName_Click(object sender, EventArgs e)
        {
            gvOutsideItemSearch.EditIndex = -1;
            Session.Remove("ItemName");
            Session.Remove("Items");
            itemSearch.searchMethod = 2;
            itemSearch.itemSearched = txtOutsideItemName.Text;
            GetItemName();
            ds = presentation.GetItemByNameByStore(itemSearch.itemSearched, GetStoreID());
            items = ds;
            ItemList();
            populateOutsideItemGridView(items);
        }

        protected void gvOutsideItemSearch_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            items = ItemList();
            gvOutsideItemSearch.PageIndex = e.NewPageIndex;
            gvOutsideItemSearch.DataBind();
            populateOutsideItemGridView(items);
        }

        protected void gvOutsideItemSearch_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Add")
            {
                // Retrieve the row index stored in the 
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);


                // Retrieve the row that contains the button 
                // from the Rows collection.
                GridViewRow row = gvOutsideItemSearch.Rows[index];
                int Num;
                bool isNum = int.TryParse(((TextBox)row.FindControl("txtQuantity")).Text, out Num);
                if (!isNum)
                {
                    lblOutsideMyError.Text = "Enter a number";
                }
                else if (Convert.ToInt32(((TextBox)row.FindControl("txtQuantity")).Text) < 1)
                {
                    lblOutsideMyError.Text = "Enter amount greater then 0";
                }
                else if (((TextBox)row.FindControl("txtQuantity")).Text == "")
                {
                    lblOutsideMyError.Text = "Must enter a valid quantity";
                }

                else
                {
                    lblOutsideMyError.Text = "";
                    //the drop down list ddlSubUnit, or the "Order For" field is used in DB code here.
                    presentation.AddItemToRequisition(Convert.ToInt32(lblOutsideID.Text), Convert.ToInt32((row.FindControl("lblItemID") as Label).Text), Convert.ToInt32(((TextBox)row.FindControl("txtQuantity")).Text), 1);

                    ((TextBox)row.FindControl("txtQuantity")).Text = "";

                    btnSubmitRequisition.Visible = true;
                    lblRequestMissing.Text = "";
                    ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(lblOutsideID.Text), false);
                    gvOutsideRequisition.DataSource = ds.Tables[1];
                    gvOutsideRequisition.DataBind();
                }
            }
        }

        protected void gvOutsideItemSearch_Sorting(object sender, GridViewSortEventArgs e)
        {
            // Sort the data
            if (GridViewSortDirection == SortDirection.Ascending)
            {
                OutsideSortDirection = SortDirection.Descending;
                OutsideSort(e.SortExpression, "DESC");
            }
            else
            {
                OutsideSortDirection = SortDirection.Ascending;
                OutsideSort(e.SortExpression, "ASC");
            }
        }

        public SortDirection OutsideSortDirection
        {
            get
            {
                if (Session["allocationSortDirection"] == null)
                    Session["allocationSortDirection"] = SortDirection.Ascending;

                return (SortDirection)Session["allocationSortDirection"];
            }
            set { Session["allocationSortDirection"] = value; }
        }

        private void OutsideSort(String sortExpression, String direction)
        {
            itemAllocation = ItemAllocating();

            DataTable dt = itemAllocation.Tables[1];

            //Sorting with DataView
            DataView dv = new DataView(dt);
            dv.Sort = sortExpression + " " + direction;

            //replace original table into Dataset with sorted table
            itemAllocation.Tables.RemoveAt(1);
            itemAllocation.Tables.Add(dv.ToTable());

            //Call binding method
            populateGridviewByName(itemAllocation);
        }

        protected void btnSubmitOutsideProcure_Click(object sender, EventArgs e)
        {
            Page.Validate("Outside");
            if (Page.IsValid)
            {
                int outsideID = Convert.ToInt32(lblOutsideID.Text);
                int vendorID = Convert.ToInt32(ddlOutsideVendor.SelectedValue);

                presentation.InsertOutsideProcureToInventory(outsideID, txtOutsideOrderID.Text, vendorID);
                presentation.SubmitOutside(outsideID, vendorID, txtOutsideOrderID.Text);
                mvRequest.ActiveViewIndex = 0;
                Response.Redirect("RequisitionStoreManager.aspx");
            }
        }

        protected void btnCancelOutsideProcure_Click(object sender, EventArgs e)
        {
            //Check first if there are items in a request to delete
            if (gvOutsideRequisition.Rows.Count > 0)
            {
                for (int i = 1; i <= gvOutsideRequisition.Rows.Count; i++)
                {
                    int index = i - 1;
                    GridViewRow row = gvOutsideRequisition.Rows[index];
                    presentation.DeleteRequisitionItem(Convert.ToInt32(lblOutsideID.Text), Convert.ToInt32(((Label)row.FindControl("lblItemID")).Text),
                        Convert.ToInt32(((Label)row.FindControl("lblStoreID")).Text));
                }
            }
            //Then delete the request and redirect.
            presentation.CancelRequisition(Convert.ToInt32(lblOutsideID.Text));
            Response.Redirect("RequisitionStoreManager.aspx");
        }

        protected void gvOutsideRequisition_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Remove")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvOutsideRequisition.Rows[index];

                presentation.DeleteRequisitionItem(Convert.ToInt32(lblOutsideID.Text), Convert.ToInt32((row.FindControl("lblItemID") as Label).Text), Convert.ToInt32(((Label)row.FindControl("lblStoreID")).Text));

                ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(lblOutsideID.Text), false);

                if (ds.Tables[0].Rows[0][0].ToString() == "")
                {
                    gvOutsideRequisition.DataSource = ds.Tables[1];
                    gvOutsideRequisition.DataBind();
                    btnSubmitOutsideProcure.Visible = true;
                    lblOutsideRequestMissing.Text = "";
                }
                else
                {
                    gvOutsideRequisition.DataSource = null;
                    gvOutsideRequisition.DataBind();
                    btnSubmitOutsideProcure.Visible = false;
                    lblOutsideRequestMissing.Text = "There are currently no items for this request";
                }
            }
        }

        protected void gvOutsideRequisition_RowEditing(object sender, GridViewEditEventArgs e)
        {
            ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(lblOutsideID.Text), false);
            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                gvOutsideRequisition.DataSource = ds.Tables[1];
                gvOutsideRequisition.EditIndex = e.NewEditIndex;
                gvOutsideRequisition.DataBind();
                //GridViewRow row = gvRequisition.Rows[e.NewEditIndex];
                gvOutsideRequisition.Columns[3].Visible = false;
            }
            else
            {
                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                gvOutsideRequisition.DataBind();
            }
        }

        protected void gvOutsideRequisition_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvOutsideRequisition.EditIndex = -1;
            ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(lblOutsideID.Text), false);
            if (ds.Tables[0].Rows[0][0].ToString() == "")
                gvOutsideRequisition.DataSource = ds.Tables[1];
            else
                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
            gvOutsideRequisition.DataBind();
            gvOutsideRequisition.Columns[3].Visible = true;
            //GridViewRow row = gvRequisition.Rows[e.RowIndex];
            //row.Cells[5].Enabled = true;
        }

        protected void gvOutsideRequisition_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = gvOutsideRequisition.Rows[e.RowIndex];
            int qty = Convert.ToInt32(((TextBox)row.FindControl("txtEditRequisitionQty")).Text);

            presentation.ModifyRequisition(Convert.ToInt32(lblOutsideID.Text),
                Convert.ToInt32(((Label)row.FindControl("lblStoreID")).Text), Convert.ToInt32((row.FindControl("lblItemID") as Label).Text), qty);
            gvOutsideRequisition.EditIndex = -1;
            ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(lblOutsideID.Text), false);
            if (ds.Tables[0].Rows[0][0].ToString() == "")
                gvOutsideRequisition.DataSource = ds.Tables[1];
            else
                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
            gvOutsideRequisition.DataBind();
            gvOutsideRequisition.Columns[3].Visible = true;
        }

        protected void btnCreateOutsideProcure_Click(object sender, EventArgs e)
        {
            ds = presentation.AddRequisition(Convert.ToInt32(GetStoreID()), "Need to change Order ID");
            txtOutsideOrderID.Text = "";
            mvRequest.ActiveViewIndex = 2;
        }
    }
}